package com.czg.jsonschema2lombokpojo.annotator;

import static org.apache.commons.lang3.StringUtils.isNotEmpty;

import com.czg.jsonschema2lombokpojo.config.GenerationConfig;
import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;

import com.czg.jsonschema2lombokpojo.rules.FormatRule;


import jakarta.json.bind.annotation.JsonbDateFormat;
import jakarta.json.bind.annotation.JsonbProperty;

/**
 * 使用json - b1映射注解注解生成的Java类型。实现灵感来自Jackson2Annotator。
 */
public class Jsonb1Annotator extends AbstractAnnotator {

    public Jsonb1Annotator(GenerationConfig generationConfig) {
        super(generationConfig);
    }

    @Override
    public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
        field.annotate(JsonbProperty.class).param("value", propertyName);
    }

    @Override
    public boolean isAdditionalPropertiesSupported() {
        return true;
    }

    @Override
    public void dateField(JFieldVar field, JDefinedClass clazz, JsonNode node) {
        String pattern = null;
        if (node.has("customDatePattern")) {
            pattern = node.get("customDatePattern").asText();
        } else if (node.has("customPattern")) {
            pattern = node.get("customPattern").asText();
        } else if (isNotEmpty(getGenerationConfig().getCustomDatePattern())) {
            pattern = getGenerationConfig().getCustomDatePattern();
        } else if (getGenerationConfig().isFormatDates()) {
            pattern = FormatRule.ISO_8601_DATE_FORMAT;
        }

        if (!field.type().fullName().equals("java.lang.String")) {
            pattern = pattern != null? pattern : FormatRule.ISO_8601_DATE_FORMAT;
            field.annotate(JsonbDateFormat.class).param("value", pattern);
        }
    }

    @Override
    public void timeField(JFieldVar field, JDefinedClass clazz, JsonNode node) {
        String pattern = null;
        if (node.has("customTimePattern")) {
            pattern = node.get("customTimePattern").asText();
        } else if (node.has("customPattern")) {
            pattern = node.get("customPattern").asText();
        } else if (isNotEmpty(getGenerationConfig().getCustomTimePattern())) {
            pattern = getGenerationConfig().getCustomTimePattern();
        } else if (getGenerationConfig().isFormatDates()) {
            pattern = FormatRule.ISO_8601_TIME_FORMAT;
        }

        if (!field.type().fullName().equals("java.lang.String")) {
            pattern = pattern != null? pattern : FormatRule.ISO_8601_TIME_FORMAT;
            field.annotate(JsonbDateFormat.class).param("value", pattern);
        }
    }

    @Override
    public void dateTimeField(JFieldVar field, JDefinedClass clazz, JsonNode node) {
        String pattern = null;
        if (node.has("customDateTimePattern")) {
            pattern = node.get("customDateTimePattern").asText();
        } else if (node.has("customPattern")) {
            pattern = node.get("customPattern").asText();
        } else if (isNotEmpty(getGenerationConfig().getCustomDateTimePattern())) {
            pattern = getGenerationConfig().getCustomDateTimePattern();
        } else if (getGenerationConfig().isFormatDateTimes()) {
            pattern = FormatRule.ISO_8601_DATETIME_FORMAT;
        }

        if (!field.type().fullName().equals("java.lang.String")) {
            pattern = pattern != null? pattern : FormatRule.ISO_8601_DATETIME_FORMAT;
            field.annotate(JsonbDateFormat.class).param("value", pattern);
        }
    }
}
